package com.sec.android.app.myfiles.listener;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;

import com.sec.android.app.myfiles.feature.DragAndDropMgr;
import com.sec.android.app.myfiles.fragment.AbsMyFilesFragment;
import com.sec.android.app.myfiles.info.AppConstants;
import com.sec.android.app.myfiles.listener.ListenerMgr.LifeCycle;
import com.sec.android.app.myfiles.log.Log;

public class DragAndDropReceiver extends AbsBroadcastReceiverImp {
    AbsMyFilesFragment mFragment;
    DragAndDropMgr mDragDrop;

    public DragAndDropReceiver(Context context, AbsMyFilesFragment fragment, DragAndDropMgr dragDrop, LifeCycle registerTime,
            LifeCycle unregisterTime) {
        super(context, registerTime, unregisterTime);
        mFragment = fragment;
        mDragDrop = dragDrop;
    }

    @Override
    IntentFilter getIntentFilter() {
        if (mIntentFilter == null) {
            mIntentFilter = new IntentFilter(AppConstants.MYFILES_ACTION_DRAG_AND_DROP_FINISH);
        }
        return mIntentFilter;
    }

    @Override
    BroadcastReceiver getBroadcastReceiver() {
        if (mReceiver == null) {
            mReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (intent.getAction().equals(AppConstants.MYFILES_ACTION_DRAG_AND_DROP_FINISH)) {
                        Log.d("MYFILES", "Drag & drop is finished");

                        if (mDragDrop.isDragStart()) {
                            mFragment.finishActionMode();
                            mDragDrop.setDragStart(false);
                        }
                    }
                }
            };
        }
        return mReceiver;
    }

    @Override
    public void registerListener() {
        LocalBroadcastManager.getInstance(mContext).registerReceiver(mReceiver, mIntentFilter);
    }

    @Override
    public void unregisterListener() {
        LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mReceiver);
    }
}
